package com.itheima.aspect;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Operation;
import com.itheima.service.OperationService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * @author Ossian
 * @version v1.0
 * @date 2019/7/5 18:00
 * @description 自定义切面类,记录日志
 **/
@Aspect
@Component
public class MyAspectXml {
    @Reference
    private OperationService operationService;

    //获取框架中的用户名

    private String getUserName() {
        return ((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername();
    }

    @AfterReturning("execution(* com.itheima.controller.*.*(..))")
    public void writeLog(JoinPoint joinPoint) {
        //获取操作者名字
        String operator = getUserName();

        //获取当前时间
        Date date = Date.from(((LocalDateTime.now()).atZone(ZoneId.systemDefault())).toInstant());

        //获取正在执行的方法名
        String methodName = joinPoint.getSignature().getName();

        //获取方法中的形参
        Object[] args = joinPoint.getArgs();

        //获取类名
        String[] split = joinPoint.getTarget().getClass().getName().split("\\.");
        String className =split [split.length-1];

        //判断对应的controller进行加强
        switch(className){
            case "CheckitemController":
                if (methodName.startsWith("delete")) {
                    operationService.addOperation(new Operation(operator,date,"删除检查项","删除",(Integer) args[0]));
                } else if (methodName.startsWith("edit")) {
                    operationService.addOperation(new Operation(operator,date,"编辑检查项","编辑",((CheckItem) args[0]).getId()));
                } else if (methodName.startsWith("add")) {
                    operationService.addOperation(new Operation(operator,date,"新增检查项","添加",null));
                } else {
                    operationService.addOperation(new Operation(operator,date,"查询检查项","查询",null));
                }
                break;
            case "CheckgroupController":
                if (methodName.startsWith("delete")) {
                    operationService.addOperation(new Operation(operator,date,"删除检查组","删除",(Integer) args[0]));

                } else if (methodName.startsWith("edit")) {
                    operationService.addOperation(new Operation(operator,date,"编辑检查组","编辑",((CheckGroup) args[0]).getId()));
                } else if (methodName.startsWith("add")) {
                    operationService.addOperation(new Operation(operator,date,"新增检查组","添加",null));
                } else {
                    operationService.addOperation(new Operation(operator,date,"查询检查组","查询",null));
                }
                break;
            case "SetmealController":
                if (methodName.startsWith("delete")) {
                    operationService.addOperation(new Operation(operator,date,"删除套餐","删除",(Integer) args[0]));
                } else if (methodName.startsWith("edit")) {
                    operationService.addOperation(new Operation(operator,date,"编辑套餐","编辑",((CheckGroup) args[0]).getId()));
                } else if (methodName.startsWith("add")) {
                    operationService.addOperation(new Operation(operator,date,"新增套餐","添加",null));
                } else {
                    operationService.addOperation(new Operation(operator,date,"查询套餐","查询",null));
                }
                break;
            case "OrdersettingController":
                operationService.addOperation(new Operation(operator,date,"预约设置","预约",null));
            default:
                break;
        }
    }
}